﻿using System;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz;
using QuanLyThuPhiTaiLieu.Biz.QuanTriHeThong;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using Telerik.WinControls.UI;

namespace QuanLyThuPhiTaiLieu.Forms.QuanTriHeThong
{
    public partial class PhanQuyen : Form
    {
        private int id;        

        public PhanQuyen()
        {
            InitializeComponent();
        }

        private void SaveItem(object sender, EventArgs e)
        {
            if (Validate())
            {
                string message = string.Empty;

                var selectedItem = ddlAccount.SelectedItem;
                if (selectedItem != null)
                {
                    var user = selectedItem.Value as NguoiSuDung;
                    if (user != null)
                    {
                        var biz = new NguoiSuDungBiz();
                        biz.UpdateRoles(user.Id, GetRoles());

                        message = Constants.Messages.UpdateCompleted;
                        MessageBox.Show(message);
                    }
                }
                
                Utilities.CloseForm(this, new ViewNguoiSuDung());
            }
        }

        private void Cancel(object sender, EventArgs e)
        {
            Utilities.CloseForm(this, new ViewNguoiSuDung());
        }

        private new bool Validate()
        {
            bool hasError = true;
            bool isFocused = false;
            errorProvider.Clear();

            if (ddlAccount.SelectedItem != null && string.IsNullOrEmpty(Convert.ToString(ddlAccount.SelectedItem.Value)))
            {
                errorProvider.SetError(ddlAccount, Constants.Messages.RequireMessage);
                hasError = false;

                ddlAccount.Focus();
                isFocused = true;
            }            

            return hasError;
        }

        private void FormLoad(object sender, EventArgs e)
        {
            var parent = MdiParent as MDI;
            if (parent != null)
            {
                Utilities.ResetBackColor(parent.TopMenu, Constants.MenuIndex.QuanTri);
                parent.Status.Text = string.Empty;
            }

            LoadAccount();
        }

        private void ResetPassword(object sender, EventArgs e)
        {
            
        }

        private void SelectAccount(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            cboDanhMuc.Checked = false;
            cboDanhMucBoSung.Checked = false;
            cboDanhMucChiTiet.Checked = false;
            cboKhaiThacVaSuDungTaiLieu.Checked = false;
            cboPhieuBanGiao.Checked = false;
            cboThamKhaoTaiLieu.Checked = false;

            var selectedItem = ddlAccount.SelectedItem;
            if (selectedItem != null)
            {
                var user = selectedItem.Value as NguoiSuDung;
                if (user != null)
                {
                    textHoVaTen.Text = user.HoVaTen;
                    textDonVi.Text = user.DonVi;
                    if (!string.IsNullOrEmpty(user.Roles))
                    {
                        SelectRoles(user.Roles);
                    }
                }
            }
        }

        private void LoadAccount()
        {
            var biz = new NguoiSuDungBiz();
            var items = biz.ViewAll();

            ddlAccount.Items.Clear();

            foreach (NguoiSuDung user in items)
            {
                var item = new RadListDataItem(user.Account, user);
                ddlAccount.Items.Add(item);
            }            
        }

        private void SelectRoles(string roles)
        {
            string template = "{0};#";
            
            if (roles.IndexOf(string.Format(template, Constants.Roles.ThamKhaoTaiLieu)) != -1)
            {
                cboThamKhaoTaiLieu.Checked = true;
            }

            if (roles.IndexOf(string.Format(template, Constants.Roles.KhaiThacVaSuDungTaiLieu)) != -1)
            {
                cboKhaiThacVaSuDungTaiLieu.Checked = true;
            }

            if (roles.IndexOf(string.Format(template, Constants.Roles.PhieuBanGiao)) != -1)
            {
                cboPhieuBanGiao.Checked = true;
            }

            if (roles.IndexOf(string.Format(template, Constants.Roles.DanhMuc)) != -1)
            {
                cboDanhMuc.Checked = true;
            }

            if (roles.IndexOf(string.Format(template, Constants.Roles.DanhMucChiTiet)) != -1)
            {
                cboDanhMucChiTiet.Checked = true;
            }

            if (roles.IndexOf(string.Format(template, Constants.Roles.DanhMucBoSung)) != -1)
            {
                cboDanhMucBoSung.Checked = true;
            }
        }

        private string GetRoles()
        {
            string roles = string.Empty;
            string template = "{0};#";

            if (cboThamKhaoTaiLieu.Checked)
            {
                roles = string.Concat(roles, string.Format(template, Constants.Roles.ThamKhaoTaiLieu));
            }

            if (cboKhaiThacVaSuDungTaiLieu.Checked)
            {
                roles = string.Concat(roles, string.Format(template, Constants.Roles.KhaiThacVaSuDungTaiLieu));
            }

            if (cboPhieuBanGiao.Checked)
            {
                roles = string.Concat(roles, string.Format(template, Constants.Roles.PhieuBanGiao));
            }

            if (cboDanhMuc.Checked)
            {
                roles = string.Concat(roles, string.Format(template, Constants.Roles.DanhMuc));
            }

            if (cboDanhMucChiTiet.Checked)
            {
                roles = string.Concat(roles, string.Format(template, Constants.Roles.DanhMucChiTiet));
            }

            if (cboDanhMucBoSung.Checked)
            {
                roles = string.Concat(roles, string.Format(template, Constants.Roles.DanhMucBoSung));
            }

            return roles;
        }
    }
}
